package com.easy.common.web;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easy.auth.domain.Waring;
import com.easy.auth.service.WaringService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.tool.email.MailSenderInfo;
import com.easy.common.tool.email.SimpleMailSender;
import com.easy.common.util.DateUtil;
import com.easy.system.domain.Lov;
import com.easy.system.service.LovService;

@Service("easyTimerTask")
public class EasyTimerTask{
	@Autowired
	private WaringService waringService;

	@Autowired
	private LovService lovService;
	
	
	public void sendEmail() throws ParseException{
		
			Waring waring=new Waring();
			PageInfo pageInfo=new PageInfo(999);
			List<Waring>waringList=waringService.getAllWarings(waring,pageInfo).getRows();
			
			for(int i=0;i<waringList.size();i++){
				String life=waringList.get(i).getLife();
				String waring_earlytime=waringList.get(i).getWaring_earlytime();
				String changeTime=waringList.get(i).getChange_time();
				String startTime = changeTime;
				if(changeTime==null||changeTime.equals("")){
					startTime = waringList.get(i).getStart_time();
				}
				Date startDate = DateUtil.getDate(startTime, "yyyy-MM-dd");
				int lifeMonth = Integer.parseInt(waringList.get(i).getLife());
				int waringDay = -Integer.parseInt(waringList.get(i).getWaring_earlytime());
				Date waringDate = getDateSomeMonth(startDate,lifeMonth,waringDay);
				Date dieDate = getDateSomeMonth(startDate,lifeMonth,0);
				boolean isWaring = waringDate.compareTo(new Date())<0; 
				boolean isDie = dieDate.compareTo(new Date())<0; 
				String subject = "夹具单元即将";
				String content = "这是一封夹具报警测试邮件，多有打扰，敬请谅解！";
				if(isDie){//发送已经过期邮件
					subject="夹具单元："+waringList.get(i).getJiaju_no()+"已经超过使用寿命";
					content = "夹具单元："+waringList.get(i).getJiaju_name()+"已经超过使用寿命，如果该单元已经放弃使用，请删除该记录";
					//sendWaringEmail(subject,content);
				}else if(isWaring){//发送提醒邮件
					subject="夹具单元："+waringList.get(i).getJiaju_no()+"即将到达使用寿命";
					content = "夹具单元："+waringList.get(i).getJiaju_name()+"("+waringList.get(i).getJiaju_no()+
							")已经即将达到使用寿命，如果已经更换，请在系统上更换该夹具单元";
					//sendWaringEmail(subject,content);
				}
//				System.out.println(isWaring); 
//				System.out.println(isDie); 
//				System.out.println(DateUtil.getDateStringByformat( "yyyy-MM-dd", waringDate)+"不继承QuartzJob"+DateUtil.getSystemDate()+"Bean方式-调度进行中..."+changeTime);
			}
			
			String subject = "夹具单元即将";
			String content = "这是一封夹具报警测试邮件，多有打扰，敬请谅解！";
			
		
			
		}
	public  Date getDateSomeMonth(Date d, int month,int day) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);  
        return now.getTime();  
    }    
	
	

	public  void sendWaringEmail(String subject,String content){
	//这个类主要是设置邮件
		String fromAddress = "dpv_waring@fawcar.com.cn";
		String toAddress = "	liuyilong@fawcar.com.cn";

		List<Lov> fromAddressList =lovService.getLovBysubType("email.FromAddress");
		List<Lov> toAddressList =lovService.getLovBysubType("email.ToAddress");
		if(fromAddressList!=null&&fromAddressList.size()==1){
			fromAddress = fromAddressList.get(0).getOname();
		} 
		if(toAddressList!=null&&toAddressList.size()>0){
			toAddress="";
		} 
		for (int i = 0; i < toAddressList.size(); i++) {
			if(i==0){
				toAddress = toAddressList.get(i).getOname();
			}else{
				toAddress = toAddress+","+toAddressList.get(i).getOname();
			}
		}
		
		MailSenderInfo mailInfo=new MailSenderInfo();
		mailInfo.setMailServerHost("10.135.33.1");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(false);
//		mailInfo.setUserName("han2000lei@163.com");
//		mailInfo.setPassword("**********");//您的邮箱密码
		mailInfo.setFromAddress(fromAddress);
		mailInfo.setToAddress(toAddress);
		mailInfo.setSubject(subject);//"设置邮箱标题如http://www.guihua.org中国桂花网");
		mailInfo.setContent(content);//"设置邮箱内容如http://www.guihua.org中国桂花网是中国最大桂花网站==");
		//这个类主要来发送邮件
		SimpleMailSender sms=new SimpleMailSender();
		sms.sendTextMail(mailInfo);//发送文体格式
		SimpleMailSender.sendHtmlMail(mailInfo);//发送html格式
	}

}
